package com.fulihui.wgj.service.subdetail;

import com.alibaba.dubbo.config.annotation.Service;
import com.fulihui.common.builder.rpc.RpcResultBuilder;
import com.fulihui.common.conv.BeanConv;
import com.fulihui.common.error.Errors;
import com.fulihui.common.error.InvokeServiceException;
import com.fulihui.common.result.rpc.BaseResult;
import com.fulihui.common.result.rpc.SimpleResult;
import com.fulihui.common.util.ConditionValidator;
import com.fulihui.wgj.dal.mybatis.domain.WxSubUserDetail;
import com.fulihui.wgj.dal.mybatis.repository.WxSubUserDetailRepository;
import com.fulihui.wgj.facade.dto.WxSubUserDetailDTO;
import com.fulihui.wgj.facade.request.subscribe.WxSubUserDetailRequest;
import com.fulihui.wgj.facade.request.subscribe.WxSubUserDetailUpdateRequest;
import com.fulihui.wgj.facade.service.subdetail.WxSubUserDetailService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 用户关注详情Service
 *
 * @author wang_wx
 * @date 2018-07-04
 */
@Component
@Service(interfaceClass = WxSubUserDetailService.class, version = "1.0.0")
public class WxSubUserDetailServiceImpl implements WxSubUserDetailService {

    private static final Logger LOGGER = LoggerFactory.getLogger(WxSubUserDetailServiceImpl.class);

    @Autowired
    WxSubUserDetailRepository wxSubUserDetailRepository;

    @Override
    public SimpleResult<WxSubUserDetailDTO> queryByAppIdAndOpenid(String appId, String openid) {
        SimpleResult result = new SimpleResult<List<WxSubUserDetailDTO>>();
        if (StringUtils.isNotBlank(appId)) {
            WxSubUserDetail wxSubUserDetail = wxSubUserDetailRepository.selectByOpenId(appId, openid);
            if (Objects.nonNull(wxSubUserDetail)) {
                WxSubUserDetailDTO subUserDetailDTO = BeanConv.copy(wxSubUserDetail, WxSubUserDetailDTO.class);
                result = RpcResultBuilder.succSimple(subUserDetailDTO);
            }
        }
        return result;
    }

    @Override
    public BaseResult saveWxSubUserDetail(WxSubUserDetailRequest request) throws InvokeServiceException {
        ConditionValidator.validate(request);
        WxSubUserDetail subUserDetail = BeanConv.copy(request, WxSubUserDetail.class);
        subUserDetail.setGmtModified(new Date());
        Integer count = wxSubUserDetailRepository.saveSelective(subUserDetail);
        return count > 0 ? RpcResultBuilder.succ() : RpcResultBuilder.fail(Errors.Commons.SAVE_FAILED);
    }

    @Override
    public BaseResult updateDetailById(WxSubUserDetailUpdateRequest request) throws InvokeServiceException {
        ConditionValidator.validate(request);
        WxSubUserDetail subUserDetail = BeanConv.copy(request, WxSubUserDetail.class);
        subUserDetail.setGmtModified(new Date());
        Integer count = wxSubUserDetailRepository.updateSelective(subUserDetail);
        return count > 0 ? RpcResultBuilder.succ() : RpcResultBuilder.fail(Errors.Commons.UPDATE_FAILED);
    }
}
